package com.ccys.cms.controller.administrator;

import com.ccys.cms.service.UserService;
import com.ccys.common.constants.ContextConstants;
import com.ccys.common.constants.TouristApi;
import com.ccys.common.enums.CodeEnum;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.web.base.ModuleDto;
import com.ccys.db.dto.web.base.SysUserDto;
import com.ccys.db.entity.SysModule;
import com.ccys.db.entity.TblAccount;
import com.ccys.db.mapper.SysModuleMapper;
import com.ccys.db.vo.web.base.ModuleSelectVo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.ccys.common.vo.ResponseResult.error;
import static com.ccys.common.vo.ResponseResult.success;

/**
 * 授权登录
 */
@RestController
@RequestMapping("/administrator")
@Api(tags = "系统-后台登录模块")
public class LoginAuthController {
    @Resource
    private UserService userService;
    @Resource
    private SysModuleMapper moduleMapper;

    /**
     * PC登录
     * @return
     * @throws Exception
     */
    @TouristApi
    @PostMapping("/loginWeb")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "登录账号",paramType = "query"),
            @ApiImplicitParam(name = "userPassword", value = "密码",paramType = "query"),
    })
    public ResponseResult login( @ApiIgnore SysUserDto userDto){
        return userService.login(userDto);
    }

    /**
     * PC退出登录
     * @return
     */
    @PostMapping("/exitLoginPC")
    public ResponseResult exitLoginPC(){
        return userService.exitLogin(Kit.getRequest());
    }

    @GetMapping("/loginModules")
    @ApiOperation("获取模块列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = ContextConstants.MODEL_DESC,response = ModuleSelectVo.class)
    })
    public ResponseResult manage(@ApiIgnore ModuleDto moduleDto) {
        TblAccount curLoginUser = UserUtil.getCurLoginAccount();
        if (curLoginUser==null) {
            return error(CodeEnum.NO_AUTH);
        }
        //开发者账号和超级管理员查看全部菜单
        if("dev".equals(curLoginUser.getUserType())||"administrator".equals(curLoginUser.getUserType())){
            SysModule module = new SysModule();
            module.setDelFlag(0);
            return success(moduleMapper.queryAll());
        }
        moduleDto.setUserId(curLoginUser.getId());
        return success(moduleMapper.queryLoginModules(moduleDto));
    }
}
